$("#registerBox").focusout(function() {
	$('.error').hide();
	$('#errorMsg').hide();
});

String.prototype.startsWith = function(str) {
	return (this.indexOf(str) === 0);
};

function login(requestUrl) {
	// Get the form values
	var logon = $('input[name="login"]').val();
	var password = $('input[name="password"]').val();

	var errorFound = false;

	if (!checkLoginName()) {
		errorFound = true;
	}

	if (!checkLoginPassword() || errorFound) {
		errorFound = true;
	}

	if (errorFound) {
		return;
	} else {
		clearErrorMsg();
	}

	$.post('/CloudContact/Login', "login=" + logon + "&password=" + password
			+ "&requestUrl=" + requestUrl, function(data) {
		if (!(data.startsWith("Error:"))) {
			document.location = data;
		} else {
			showError("#errorMsg", data);
		}
	});
}

function checkLoginName() {
	var logon = $('input[name="login"]').val();

	if (logon == "") {
		showError('#loginNameError', "User name cannot be blank");
		return false;
	} else {
		$('#loginNameError').hide();
		return true;
	}
}

function checkLoginPassword() {
	var password = $('input[name="password"]').val();

	if (password == "") {
		showError('#loginPasswordError', "Password cannot be blank");
		return false;
	} else {
		$('#loginPasswordError').hide();
		return true;
	}
}

function signup() {
	$('#loginBox').hide();
	$('#registerBox').show();
}

function showError(errElement, errMsg) {
	$(errElement).text("* " + errMsg);
	$(errElement).show();
	return;
}

/*
 * http://www.sbrian.com/2011/01/javascript-email-address-validation.html
 */
function checkLoginSignUp() {
	var username = $('input[name="loginSignUp"]').val();

	if (username == "") {
		showError('#loginError', "User name cannot be blank");
		return false;
	} else if (!goog.format.EmailAddress.isValidAddress(username)) {
		showError('#loginError', "User name must be a valid email.");
		return false;
	} else {
		$('#loginError').hide();
		return true;
	}

}

function checkPasswordSignUp() {
	var password = $('input[name="passwordSignUp"]').val();

	if (password == "") {
		showError('#passwordError', "Password cannot be blank");
		return false;
	} else {
		$('#passwordError').hide();
		return true;
	}
}

function checkConfirmPassword() {
	var password = $('input[name="passwordSignUp"]').val();
	var confirmPassword = $('input[name="confirmPassword"]').val();

	if (password != confirmPassword) {
		showError('#confirmPasswordError', "Password does not match.");
		return false;
	} else {
		$('#confirmPasswordError').hide();
		return true;
	}
}

function register() {
	var username = $('input[name="loginSignUp"]').val();
	var password = $('input[name="passwordSignUp"]').val();
	var confirmPassword = $('input[name="confirmPassword"]').val();

	var errorFound = false;

	if (!checkLoginSignUp()) {
		errorFound = true;
	}

	if (!checkPasswordSignUp() || errorFound) {
		errorFound = true;
	}

	if (!checkConfirmPassword() || errorFound) {
		errorFound = true;
	}

	if (errorFound) {
		return;
	} else {
		clearErrorMsg();
	}

	$
			.post(
					'/CloudContact/Register',
					"userName=" + username + "&password=" + password,
					function(data) {
						if (data.startsWith("Critical:")) {
							alert(data);
						} else if (data.startsWith("Error:")) {
							showError("#errorMsg", data);
						} else if (data.startsWith("Success:")) {
							window.location.href = removeParameter("/CloudContact/activate.jsp");
						}
					});
}

function activate() {
	var id = $('input[name="id"]').val();
	var username = $('input[name="userName"]').val();
	var code = $('input[name="activateCode"]').val();

	if ((username == "" && id == "") || !checkActivateCode()) {
		return;
	}

	$
			.post(
					'/CloudContact/Activate',
					"userName=" + username + "&code=" + code + "&id=" + id,
					function(data) {
						if (data.startsWith("Critical:")) {
							alert(data);
						} else if (data.startsWith("Error:")) {
							showError("#errorActivate", data);
						} else if (data.startsWith("Success:")) {
							window.location.href = removeParameter("/CloudContact/activated.jsp");
						}
					});
}

function checkActivateUserName() {
	var userName = $('input[name="userName"]').val();

	if (userName == "") {
		showError('#usernameError', "User name cannot be blank");
		return false;
	} else {
		$('#usernameError').hide();
		return true;
	}
}

function checkActivateCode() {
	var code = $('input[name="activateCode"]').val();

	if (code == "") {
		showError('#codeError', "Activatation code cannot be blank");
		return false;
	} else {
		$('#codeError').hide();
		return true;
	}
}

function clearErrorMsg() {
	$('#loginNameError').hide();
	$('#loginPasswordError').hide();
	$('#loginError').hide();
	$('#passwordError').hide();
	$('#confirmPasswordError').hide();
	$('#errorMsg').text("");
	$('#errorMsg').hide();
}